let hyMath = require('../../utils/math.js');
var request = require('../../utils/request.js')
var config = require('../../config.js');

// pages/can-use/can-use.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        discounts: {},
        save: '',  //储值金额
        send: '¥300.00',   //赠送金额
        right: '../../images/right.png',
        points: '¥30.00',
        totalNum: '60.00',
        maxSyn:'100000',//最大综合
        maxHour : '100000'//最大工时
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        // 接受优惠的参数；
        let discounts = {};
        discounts.total_discount = hyMath.hyToNum(options.total_discount);
        discounts.syn_discount = options.syn_discount ? options.syn_discount  :  '';
        discounts.work_discount = options.work_discount ?  options.work_discount : '';
        discounts.other_discount = options.other_discount ?  options.other_discount : '';

        discounts.work_cost = options.work_cost;
        discounts.parts_cost = options.parts_cost;
        discounts.additional_cost = options.additional_cost;
        discounts.member_id = options.member_id;

        // "work_cost":10, // 工时费用
        //     "parts_cost":20, // 零件费用
        //     "additional_cost":30, // 附加费用


        request.post({ path: config.preferentialInfo}, {'member_id' : discounts.member_id}).then(res => {
            if (res.data.ret == 0) {
                let data = res.data.data;
                debugger

                // "man_hour_coin": 0, 工时币
                    // "synthesize_coin": 0  综合币
                let  man_hour_coin = data.man_hour_coin || 0;
                let  synthesize_coin = data.synthesize_coin ||  0;

                let maxHour = 0;
                let maxSyn = 0;

                if(discounts.work_cost >man_hour_coin){
                    maxHour = man_hour_coin || 0;
                }else{
                    maxHour = discounts.work_cost || 0;
                }

                let lSyn = hyMath.hyadd(discounts.additional_cost , discounts.parts_cost);

                if(lSyn  > synthesize_coin){
                    maxSyn = synthesize_coin || 0;
                }else{
                    maxSyn = lSyn || 0;
                }

                that.setData({
                    maxSyn: maxSyn,
                    maxHour: maxHour,
                })
            }
        })
        this.setData({
            discounts: discounts,
        });
        console.log(discounts, 'discounts');
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    /**
     * input 响应模版数据到js中；
     * @param e
     * bindinput="inputKey"  data-name="submitP.2.total_discount" value="{{submitP['2'].total_discount}}"
     */
    inputKey: function (e) {
        let that = this;
        let lsData = that.data;
        let name = e.currentTarget.dataset.name;
        let nowatch = e.currentTarget.dataset.nowatch;

        let nameArr = name.split(".");
        let newVal = e.detail.value;
        let nameArrLen = nameArr.length
        for (let i = 0; i <= nameArrLen; i++) {
            let val = lsData[nameArr[i]];
            if (nameArrLen - 1 === i) {
                lsData[nameArr[i]] = newVal;
            } else {
                lsData = lsData[nameArr[i]];
            }
        }
        this.setData({
            submitP: that.data.submitP
        })

        if (nowatch) {
            that.watchAutoChage()
        }
        // debugger
    },
    //监听数据变化响应
    watchAutoChage: function () {
        let that = this;
        let data = that.data;
        let discounts = data.discounts;
        let maxSyn = data.maxSyn;
        let maxHour = data.maxHour;
        discounts.syn_discount = hyMath.hyToNum(discounts.syn_discount);
        discounts.work_discount = hyMath.hyToNum(discounts.work_discount);
        discounts.other_discount = hyMath.hyToNum(discounts.other_discount);
        discounts.total_discount = hyMath.hyTofloat2(discounts.total_discount);

        // 强行最大值的计算
        // discounts.syn_discount = discounts.syn_discount > maxSyn ? maxSyn :discounts.syn_discount;
        // discounts.work_discount = discounts.work_discount > maxHour ? maxHour : discounts.work_discount;


        let syn_discount = discounts.syn_discount;
        let work_discount = discounts.work_discount;
        let other_discount = discounts.other_discount;

        discounts.total_discount = parseInt(syn_discount) + parseInt(work_discount) + parseInt(other_discount);

        this.setData({
            discounts: discounts
        })
    },
    tapSubmit: function () {
        let that = this;
        let data = that.data;
        let maxSyn = data.maxSyn;
        let maxHour = data.maxHour;

        let discounts = data.discounts;
        let syn_discount = discounts.syn_discount;
        let work_discount = discounts.work_discount;



        if(work_discount > maxHour){
            wx.showToast({
                icon: 'none',
                title: `超出了综合可用最大值${maxHour}`
            })
            return false;
        }




        if(syn_discount > maxSyn){
            wx.showToast({
                icon: 'none',
                title: `超出了工时可用最大值${maxSyn}`
            })
            return false;
        }






        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2]; //上一个页面
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPage.setData({
            urlP: {discounts:discounts}
        })
        wx.navigateBack();
    }
})